#!/bin/bash

dir="${1:-$(pwd)}"
shift 1

if [ -z "${dir}" ]
then
    1>&2 echo "usage:"
    1>&2 echo "  ${0} <directory> [<option>]"
    exit 1
fi

2>/dev/null rmdir "${dir}"

if 2>/dev/null mkdir "${dir}"
then
  true
else
  if [ "${FORCE:-0}" -eq 0 ]
  then
    2>&1 echo "directory exists: ${dir}"
    2>&1 echo "re-run with FORCE=1 to extract anyway"
    exit 1
  fi
fi

set -ex

cd "${dir}"
exec tar xz$*

